﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Web.Http;
using System.Web.Http.SelfHost;

namespace MyService
{
    public class WebAPIHelper
    {
        public HttpSelfHostConfiguration Config = null;
        public HttpSelfHostServer Server = null;

        public WebAPIHelper(string port)
        {
            Config = new HttpSelfHostConfiguration("http://localhost:" + port);
            Config.Routes.MapHttpRoute("default", "{controller}/{id}", new { id = RouteParameter.Optional });
            Server = new HttpSelfHostServer(Config);
        }

        public void Open()
        {
            if (Server != null)
            {
                Server.OpenAsync().Wait();
                Log.WriteLog("WebAPI Open OK...");
            }
        }


        public void Close()
        {
            if (Server != null)
            {
                Server.CloseAsync().Wait();
                Server.Dispose();
            }
        }

    }
}
